package com.servlet;

import com.alibaba.fastjson.JSON;
import com.constants.Constants;
import com.hdfs.FileDirectory;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.URI;
import java.util.List;


public class MyStreamServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;


	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);

	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			preview(req, resp);
			direc(req,resp);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void preview(HttpServletRequest req, HttpServletResponse resp) throws ClassNotFoundException, IOException {
		String fpath = req.getParameter("fpath");
		if (fpath == null)
			return;
		String filename = Constants.HDFSAddress + fpath;
		Configuration config = new Configuration();
		FileSystem fs = null;
		FSDataInputStream in = null;
		try {
			fs = FileSystem.get(URI.create(filename), config);
			in = fs.open(new Path(filename));
		} catch (IOException e) {
			e.printStackTrace();
		}
		final long fileLen = fs.getFileStatus(new Path(filename)).getLen();
		String range = req.getHeader("Range");
		resp.setHeader("Content-type", "video/mp4");
		OutputStream out = resp.getOutputStream();
		if (range == null) {
			filename = fpath.substring(fpath.lastIndexOf("/") + 1);
			resp.setHeader("Content-Disposition", "attachment; filename=" + filename);
			resp.setContentType("application/octet-stream");
			resp.setContentLength((int) fileLen);
			IOUtils.copyBytes(in, out, fileLen, false);
		} else {
			long start = Integer.valueOf(range.substring(range.indexOf("=") + 1, range.indexOf("-")));
			long count = fileLen - start;
			long end;
			if (range.endsWith("-"))
				end = fileLen - 1;
			else
				end = Integer.valueOf(range.substring(range.indexOf("-") + 1));
			String ContentRange = "bytes " + String.valueOf(start) + "-" + end + "/" + String.valueOf(fileLen);
			resp.setStatus(206);
			resp.setContentType("video/mpeg4");
			resp.setHeader("Content-Range", ContentRange);
			in.seek(start);
			try {
				IOUtils.copyBytes(in, out, count, false);
			} catch (Exception e) {
				throw e;
			}
		}
		in.close();
		in = null;
		out.close();
		out = null;
	}

	public void direc (HttpServletRequest req, HttpServletResponse resp) throws ClassNotFoundException, IOException, ServletException {
		req.setCharacterEncoding("UTF-8");
		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html;charset=UTF-8");
		List<String> list = FileDirectory.getList();
		String jsonStr = JSON.toJSONString(list);
//		req.setAttribute("jsonStr",list);
//		req.getRequestDispatcher("/src/main/webapp/index.html").forward(req,resp);
		PrintWriter out = resp.getWriter();
		out.write(jsonStr);
		out.flush();
		out.close();


	}




}
	